#include "basecalendarwidget.h"
#include <QMenu>
#include <QToolButton>
#include <QTextCharFormat>
#include <QPainter>
#include <QLocale>

BaseCalendarWidget::BaseCalendarWidget(QCalendarWidget *parent):
    QCalendarWidget(parent)
{

}

BaseCalendarWidget::~BaseCalendarWidget()
{

}

void BaseCalendarWidget::initUI()
{
    QMenu *monthMenu = QCalendarWidget::findChild<QMenu*>();
    Q_ASSERT( nullptr != monthMenu);
    QToolButton *monthBtn = qobject_cast<QToolButton*>(monthMenu->parentWidget());
    Q_ASSERT( nullptr != monthBtn);
    //monthBtn->setMenu(nullptr);
    monthBtn->setStyleSheet("QToolButton::menu-indicator {image: none;}");

    QToolButton *prevBtn = QCalendarWidget::findChild<QToolButton*>(QLatin1String("qt_calendar_prevmonth"));
    Q_ASSERT( nullptr != prevBtn);
    QSize prevSize = prevBtn->iconSize();
    prevSize.rwidth() -= 2;
    prevSize.rheight() -= 2;
    prevBtn->setIconSize(prevSize);
    prevBtn->setIcon(QIcon(":/BackDisplay/Resource/BackDisplay/arrow_left.png"));

    QToolButton *nextBtn = QCalendarWidget::findChild<QToolButton*>(QLatin1String("qt_calendar_nextmonth"));
    Q_ASSERT( nullptr != nextBtn);
    QSize nextSize = nextBtn->iconSize();
    nextSize.rwidth() -= 2;
    nextSize.rheight() -= 2;
    nextBtn->setIconSize(nextSize);
    nextBtn->setIcon(QIcon(":/BackDisplay/Resource/BackDisplay/arrow_right.png"));

    setStyleSheet("QCalendarWidget QWidget#qt_calendar_navigationbar {background-color: #696969;}");
}

void BaseCalendarWidget::initSetting()
{
    setLocale(QLocale(QLocale::Chinese));
    setSelectionMode(QCalendarWidget::SingleSelection);
    setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);

    QTextCharFormat format;
    format.setForeground(QColor(160, 160, 160));
    format.setBackground(QColor(255, 255, 255));
    setHeaderTextFormat(format);
    setWeekdayTextFormat(Qt::Saturday, format);
    setWeekdayTextFormat(Qt::Sunday,   format);
    setWeekdayTextFormat(Qt::Monday,   format);
    setWeekdayTextFormat(Qt::Tuesday,  format);
    setWeekdayTextFormat(Qt::Wednesday,format);
    setWeekdayTextFormat(Qt::Thursday, format);
    setWeekdayTextFormat(Qt::Friday,   format);
}

/* 需要根据自己的业务逻辑处理当前日期绘制的逻辑 */
void BaseCalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{
   painter->save();
   painter->setRenderHint(QPainter::Antialiasing);

   /* 绘制背景 */
   painter->setPen(Qt::NoPen);
   painter->setBrush(QColor(0, 145, 255));
   painter->drawRoundedRect(rect.x()+1, rect.y()+1, rect.width()-2, rect.height()-2, 3, 3);

   /* 绘制前景 */
   QFont dateFont = painter->font();
   dateFont.setPixelSize(rect.height()-10);
   painter->setFont(dateFont);
   painter->setPen(QColor(255, 255, 255));
   QRect dateRect = QRect(rect.x()+3, rect.y()+3, rect.width()-6, rect.height()-6);
   painter->drawText(dateRect, Qt::AlignCenter, QString::number(date.day()));

   painter->restore();
}
